/* 
 * Copyright (c) 2002, Cameron Zemek
 * 
 * This file is part of JSpread.
 * 
 * JSpread is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * JSpread is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package com.capsicumcorp.swing.spreadsheet;

import java.io.*;
import java.util.*;

import net.sf.jeppers.grid.*;

/**
 * WorkbookCodec allows serialization and deserialization of <code>JWorkbook</code>. This
 * class is necessary due to bugs in using normal serialization (even when using custom 
 * serialization methods) when the component is displayed.
 * 
 * @author <a href="grom@capsicumcorp.com">Cameron Zemek</a>
 */
public class WorkbookCodec {    
	public static void encode(OutputStream outStream, JWorkbook workbook)
		throws IOException {
		ObjectOutputStream out =
			new ObjectOutputStream(new BufferedOutputStream(outStream));

		/* Workbook header information */
		out.writeInt(workbook.getSheetCount());

		/* Worksheets */
		Collection sheetNames = workbook.getSheetNames();
		Iterator i = sheetNames.iterator();
		while (i.hasNext()) {
			String sheetName = (String) i.next();
			JSpread sheet = workbook.getSheet(sheetName);

			/* Worksheets header information */
			int rowSize = sheet.getRowCount();
			int colSize = sheet.getColumnCount();
			boolean showHeader = sheet.getShowHeader();
			boolean showGrid = sheet.getShowGrid();

			out.writeUTF(sheetName);
			out.writeInt(rowSize);
			out.writeInt(colSize);
			out.writeBoolean(showHeader);
			out.writeBoolean(showGrid);
		}

        // write out worksheets
		i = workbook.getSheetNames().iterator();
		while (i.hasNext()) {
			String sheetName = (String) i.next();
			JSpread sheet = workbook.getSheet(sheetName);
			JGrid grid = sheet.getGrid();
            
            out.writeObject(grid.getGridModel());
            out.writeObject(grid.getSpanModel());
            out.writeObject(grid.getStyleModel());
            out.writeObject(grid.getRowModel());
            out.writeObject(grid.getColumnModel());
            out.writeObject(grid.getSelectionModel());
		}

		out.flush();
	} //end encode

	public static JWorkbook decode(InputStream inStream)
		throws ClassNotFoundException, IOException {
		JWorkbook workbook = new JWorkbook();
		ObjectInputStream in =
			new ObjectInputStream(new BufferedInputStream(inStream));

		/* Workbook header information */
		int sheetCount = in.readInt();

		/* Worksheets */
		for (int i = 0; i < sheetCount; i++) {
			/* Worksheets header information */
			String sheetName = in.readUTF();
			int rowSize = in.readInt();
			int colSize = in.readInt();
			boolean showHeader = in.readBoolean();
			boolean showGrid = in.readBoolean();

			// Add worksheet
			JSpread sheet = new JSpread(rowSize, colSize);
			sheet.setShowHeader(showHeader);
			sheet.setShowGrid(showGrid);
			workbook.addSheet(sheetName, sheet);
		}

        // read in workbooks
		Iterator i = workbook.getSheetNames().iterator();
		while (i.hasNext()) {
			String sheetName = (String) i.next();
			JSpread sheet = workbook.getSheet(sheetName);
			JGrid grid = sheet.getGrid();
            
            grid.setGridModel((DefaultGridModel) in.readObject());
            grid.setSpanModel((DefaultSpanModel) in.readObject());
            grid.setStyleModel((DefaultStyleModel) in.readObject());
            grid.setRowModel((DefaultRulerModel) in.readObject());
            grid.setColumnModel((DefaultRulerModel) in.readObject());
            grid.setSelectionModel((DefaultSelectionModel) in.readObject());
		}
		
		workbook.revalidate();
		workbook.repaint();
		return workbook;
	} //end decode
} //end Workbook Codec
